package com.syqc.videos.players

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.ImageView
import android.widget.TextView
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.shuyu.gsyvideoplayer.video.base.GSYVideoViewBridge
import com.syqc.monitor.R
import com.syqc.videos.videomanager.TapeManager

class TapePlatPlayer : StandardGSYVideoPlayer {
    constructor(context: Context, fullFlag: Boolean) : super(context, fullFlag)
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)


    //数据类型
    private var mType: Int = 0

    //是否需要静音
    private var isNeedMute = true

    //进度条最大值
    private var duration = 10L

    private lateinit var ivSwitchVoice: ImageView
    private lateinit var tvSwitchScale: TextView

    override fun getLayoutId(): Int {
        return R.layout.tape_video_plat
    }

    override fun init(context: Context?) {
        super.init(context)
        initParams()
        initView()
        setListener()
        setSeekbarState()
    }

    private fun initParams() {
        isRotateViewAuto = true
        isLockLand = true
        isReleaseWhenLossAudio = false
        isShowFullAnimation = true
        setIsTouchWiget(false)
        setIsTouchWigetFull(false)

        isNeedMute = true
        TapeManager.instance.isNeedMute = isNeedMute
    }

    private fun initView() {
        ivSwitchVoice = findViewById(R.id.ivSwitchVoice)
        tvSwitchScale = findViewById(R.id.tvSwitchScale)
        ivSwitchVoice.isSelected = isNeedMute
    }

    private fun setListener() {
        //声音
        ivSwitchVoice.setOnClickListener {
            isNeedMute = !isNeedMute
            ivSwitchVoice.isSelected = isNeedMute
            TapeManager.instance.isNeedMute = isNeedMute
        }
        //比例
        tvSwitchScale.setOnClickListener {
            if (!mHadPlay) {
                return@setOnClickListener
            }
            when (mType) {
                0 -> mType = 1
                1 -> mType = 2
                2 -> mType = 3
                3 -> mType = 4
                4 -> mType = 0
            }
            resolveTypeUI()
        }
    }

    /**
     * 显示比例
     * 注意，GSYVideoType.setShowType是全局静态生效，除非重启APP。
     */
    private fun resolveTypeUI() {
        if (!mHadPlay) {
            return
        }
        when (mType) {
            1 -> {
                tvSwitchScale.text = "16:9"
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_16_9)
            }
            2 -> {
                tvSwitchScale.text = "4:3"
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_4_3)
            }
            3 -> {
                tvSwitchScale.setText(R.string.desc_video_full_screen)
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
            }
            0 -> {
                tvSwitchScale.setText(R.string.desc_video_default_ratio)
                GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
            }
        }
        changeTextureViewShowType()
        if (mTextureView != null) mTextureView.requestLayout()
    }

    ///////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 设置播放按钮图标
     */
    override fun updateStartImage() {
        if (mStartButton == null) return
        if (mStartButton is ImageView) {
            val imageView = mStartButton as ImageView
            when (mCurrentState) {
                GSYVideoView.CURRENT_STATE_PLAYING -> {
                    imageView.setImageResource(R.mipmap.video_pause_pressed)
                }
                GSYVideoView.CURRENT_STATE_ERROR -> {
                    imageView.setImageResource(R.mipmap.video_play_normal)
                }
                else -> {
                    imageView.setImageResource(R.mipmap.video_play_normal)
                }
            }
        }
    }

    override fun getGSYVideoManager(): GSYVideoViewBridge {
        TapeManager.instance.initContext(context.applicationContext)
        return TapeManager.instance
    }

    override fun touchSurfaceMoveFullLogic(absDeltaX: Float, absDeltaY: Float) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY)
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false

        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false

        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false
    }

    override fun touchDoubleUp(e: MotionEvent?) {
//        super.touchDoubleUp(e)
    }

    ///////////////////////////////////////////////////////////////////////////////////////////


    fun setDuration(duration: Long) {
        this.duration = duration
    }

    override fun getDuration(): Long {
        return duration
    }

    private fun setSeekbarState() {
        mProgressBar.isClickable = false
        mProgressBar.isEnabled = false
        mProgressBar.isFocusable = false

    }
}